<template>
  <Panel>
    <template #title>
      <p>账号列表</p>
    </template>
    <template #content>
      <div>
        <el-button type="danger" @click="delmore">批量删除</el-button>
        <el-button type="primary">取消选择</el-button>
      </div>
      <el-table
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        size="mini"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column prop="account" label="账号" width="120">
        </el-table-column>
        <el-table-column prop="userGroup" label="用户组" width="120">
        </el-table-column>
        <el-table-column label="创建时间" width="150">
          <template v-slot="{ row }">
            {{ row.ctime | formatDate }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <el-button type="primary" size="mini" @click="editUser(row)"
              >编辑</el-button
            >
            <el-button type="danger" size="mini" @click="delAccount(row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[2, 5, 10, 15]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
      <el-dialog title="编辑账号" :visible.sync="dialogVisible" width="50%">
        <div>
          <el-form
            :model="editForm"
            status-icon
            :rules="rules"
            ref="ruleForm"
            label-width="100px"
            class="demo-ruleForm"
            size="mini"
          >
            <el-form-item label="用户名" prop="username">
              <el-input
                type="text"
                v-model="editForm.username"
                autocomplete="off"
                style="width: 193px;"
              ></el-input>
            </el-form-item>
            <el-form-item label="用户组" prop="selectVal">
              <el-select v-model="editForm.selectVal" placeholder="请选择">
                <el-option
                  v-for="item in userGroup"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-form>
        </div>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false" size="mini"
            >取 消</el-button
          >
          <el-button type="primary" @click="sureEdit" size="mini"
            >确 定</el-button
          >
        </span>
      </el-dialog>
    </template>
  </Panel>
</template>

<script>
import { getUserlist, delUser, editUser, deleteMore } from '../../api/user.js'
import { validateUserName } from '../../utils/validate'

import local from '../../utils/local.js'

export default {
  data() {
    const select = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请选择用户角色'))
      } else {
        callback()
      }
    }
    return {
      tableData: [],
      multipleSelection: [],
      currentPage: 1,
      pageSize: 5,
      total: 0,
      dialogVisible: false,
      userGroup: [
        { value: '普通管理员', label: '普通管理员' },
        { value: '超级管理员', label: '超级管理员' }
      ],
      editForm: {
        id: '',
        username: '',
        selectVal: ''
      },
      rules: {
        username: [{ validator: validateUserName, trigger: 'blur' }],
        selectVal: [{ validator: select, trigger: 'click' }]
      },
      userID: JSON.parse(local.get('users')).id
    }
  },
  created() {
    this.getList()
  },
  methods: {
    async getList() {
      if (this.tableData.length === 1 && this.currentPage !== 1) {
        this.currentPage--
      }
      const { data, total } = await getUserlist({
        currentPage: this.currentPage,
        pageSize: this.pageSize
      })
      this.tableData = data
      this.total = total
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
      this.ids = []
      this.multipleSelection.forEach(item => {
        this.ids.push(item.id)
      })
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.getList()
    },
    delAccount(id) {
      this.$confirm('此操作将永久删除该账号, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          if (id !== this.userID) {
            const data = await delUser({
              id
            })
            if (data.code === 0) {
              this.getList()
            }
          } else {
            this.$message({
              type: 'error',
              message: '不删除自己哦'
            })
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    delmore() {
      if (this.ids.length === 0) return
      this.$confirm('此操作将永久删除多条账号, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          if (!this.ids.includes(this.userID)) {
            const data = await deleteMore({
              ids: JSON.stringify(this.ids)
            })
            if (data.code === 0) {
              this.getList()
            }
          } else {
            this.$message({
              type: 'error',
              message: '所选账号中包含当前登录账号，无法删除'
            })
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    editUser(obj) {
      this.dialogVisible = true
      this.editForm.username = obj.account
      this.editForm.selectVal = obj.userGroup
      this.editForm.id = obj.id
    },
    async sureEdit() {
      const data = await editUser({
        id: this.editForm.id,
        account: this.editForm.username,
        userGroup: this.editForm.selectVal
      })
      if (data.code === 0) {
        this.dialogVisible = false
        this.getList()
      }
    }
  }
}
</script>

<style lang="less" scoped>
.el-table {
  margin: 20px 0;
}
</style>
